package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.pojo.User;
import com.boot.service.UserService;
import com.boot.mapper.UserMapper;
import com.boot.utils.JwtHelper;
import com.boot.utils.MD5Util;
import com.boot.utils.Result;
import com.boot.utils.ResultCodeEum;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyang
 * @description 针对表【news_user】的数据库操作Service实现
 * @createDate 2025-04-14 13:27:54
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<Object> login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(queryWrapper);
        if (loginUser != null) {
            if (!StringUtils.isEmpty(user.getUserPwd())
                    && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())) {
                String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
                String refreshToken = jwtHelper.createRefreshToken(Long.valueOf(loginUser.getUid()));
                return Result.ok(Map.of("token", token, "refreshToken", refreshToken));
            } else {
                return Result.build(null, ResultCodeEum.PASSWORD_ERROR);
            }
        } else {
            return Result.build(null, ResultCodeEum.USERNAME_ERROR);
        }
    }

    @Override
    public Result getUserInfo(String token) {
        if (jwtHelper.isExpiration(token)) {
            return Result.build(null, ResultCodeEum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        User user = userMapper.selectById(userId);
        user.setUserPwd(null);
        Map<String, Object> data = new HashMap<>();
        data.put("loginUser", user);
        return Result.ok(data);
    }

    @Override
    public Result checkUserName(String username) {
        return checkMethod(username);
    }

    @Override
    public Result register(User user) {
        try {
            Result result = checkMethod(user.getUsername());
            if (result.getCode() == 200) {
                user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
                int row = userMapper.insert(user);
                if (row > 0) {
                    String token = jwtHelper.createToken(Long.valueOf(user.getUid()));
                    return Result.ok(Map.of("code", 200,"token", token));
                }
                return Result.build("注册失败(update0)", ResultCodeEum.REGISTERFAILED);
            }
            return result;
        } catch (Exception e) {
            return Result.build("注册失败(system)：" + e.getMessage(), ResultCodeEum.REGISTERFAILED);
        }
    }

    public Result<Object> checkMethod(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        Long count = userMapper.selectCount(queryWrapper);
        if (count > 0) {
            return Result.build(null, ResultCodeEum.USERNAME_USED);
        }

        return Result.ok(null);
    }

    @Override
    public Result getRefreshToken(@RequestHeader String refreshToken) {
        return jwtHelper.getRefreshToken(refreshToken);
    }
}




